//
//  SCStringListPopUpView.m
//  Assembly
//
//  Created by 葛悦彣 on 2023/11/13.
//

#import "SCStringListPopUpView.h"
@interface  SCStringListPopUpView ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,strong) UITableView * tableview;
@property (nonatomic ,strong) UIView * makeView;
@property (nonatomic ,strong) void(^Block)(NSString * str);
@property (nonatomic ,strong) UILabel * TitleLab;
@property (nonatomic ,strong) NSArray <NSString *>* StringArray;

@end
@implementation SCStringListPopUpView


+ (instancetype)ShowViewWithTitle:(NSString *)title andStringArray:(NSArray <NSString *>*)StringArray andBlock:(void(^)(NSString * str))block{
    
    SCStringListPopUpView *  view = [SCStringListPopUpView new];
    view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
    view.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    [[DTKTool getCurrentVC].view addSubview:view];
    
    [view viewWithTitle:title  andStringArray:StringArray andBlock:block];
    
    return view;
}
- (void)viewWithTitle:(NSString *)title andStringArray:(NSArray <NSString *>*)StringArray andBlock:(void(^)(NSString * str))block{
    UIView * makeView = [UIView new];
    self.Block = block;
    self.StringArray = StringArray;
    self.makeView = makeView;
    makeView.backgroundColor = UIColor.whiteColor;
    makeView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH,  kwt(310) + KSafeAreaBottomHeight);
    [self addSubview:makeView];
    [DTKTool BFYJ:kwt(15) andView:makeView andCorner:UIRectCornerTopLeft|UIRectCornerTopRight andSize: CGRectMake(0, 0 ,SCREEN_WIDTH, kwt(310) + KSafeAreaBottomHeight)];
    MJWeakSelf
    makeView.addClickBlock(^(){
        [weakSelf noClick];
    });

    
    self.addClickBlock(^(){
        [weakSelf dismiss];
    });
    
    UIView * topview = [UIView new];
    topview.backgroundColor = KcolorHex(@"#F0F4F9");
    [self.makeView addSubview:topview];
    
    [topview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.offset(0);
        make.height.offset(kwt(50));
    }];
    UILabel * TitleLab = [DTKTool labelWithText:title andFont:kCFont(17) andTextColor:KcolorHex(@"#1E2425")];
    self.TitleLab = TitleLab;
    [topview addSubview:TitleLab];
    [TitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(17));
        make.centerY.offset(0);
    }];
    
    UIButton * exitBtn = [UIButton buttonWithType:0];
    exitBtn.addBackgroundimg([UIImage imageNamed:@"delete_bullet_box"]).addTarget(self,@selector(dismiss));
    [topview addSubview:exitBtn];
    [exitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(kwt(17));
        make.centerY.offset(0);
        make.right.offset(kwt(-17));
    }];
    
    [self.makeView addSubview:self.tableview];
  
    [weakSelf show];
   
    
}

- (UITableView *)tableview{
    if(!_tableview){
        _tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, kwt(50), SCREEN_WIDTH, kwt(260)) style:UITableViewStyleGrouped];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        if (@available(iOS 11.0, *)) {
            _tableview.estimatedRowHeight = 0;
            _tableview.estimatedSectionHeaderHeight = 0;
            _tableview.estimatedSectionFooterHeight = 0;
            _tableview.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        if (@available(iOS 15.0, *)) {
            _tableview.sectionHeaderTopPadding = 0;
            _tableview.fillerRowHeight = 0;
        }
        _tableview.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0.01)];
        _tableview.sectionHeaderHeight = 0.01;
        _tableview.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0.01)];
        _tableview.sectionFooterHeight = 0.01;
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableview.backgroundColor = UIColor.clearColor;
    }
    return _tableview;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.StringArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kwt(48);
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    SCStringListPopUpTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"SCStringListPopUpTableViewCell"];
    if (cell == nil) {
        cell = [[SCStringListPopUpTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SCStringListPopUpTableViewCell"];
    }
    cell.name = self.StringArray[indexPath.row];
    cell.backgroundColor = UIColor.whiteColor;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    MJWeakSelf
    cell.clickBlock = ^(NSString * _Nonnull str) {
        if(weakSelf.Block){
            weakSelf.Block(str);
        }
    };
    return cell;
}


- (void)noClick{}
- (void)show {
    [UIView animateWithDuration:0.25 animations:^{
        self.makeView.frame = CGRectMake(0, SCREEN_HEIGHT - kwt(310) - KSafeAreaBottomHeight, SCREEN_WIDTH ,  kwt(310) + KSafeAreaBottomHeight);
    }];
}
- (void)dismiss{
    [UIView animateWithDuration:0.25 animations:^{
        self.makeView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH,  kwt(310) + KSafeAreaBottomHeight);
    } completion:^(BOOL finished) {
        [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        [self removeFromSuperview];
    }];
}
@end

@interface SCStringListPopUpTableViewCell()

@property (nonatomic ,strong) UILabel * namelab;
@end
@implementation SCStringListPopUpTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        [self setupView];
    }
    return self;
}


- (void)setupView{
    
    self.backgroundColor = UIColor.clearColor;
    self.contentView.backgroundColor = UIColor.clearColor;
    self.userInteractionEnabled = YES;
    self.contentView.userInteractionEnabled = YES;
   
    
    UILabel * namelab = [DTKTool labelWithText:@"--" andFont:kFont(16) andTextColor:KcolorHex(@"#1E2425")];
    [self.contentView addSubview:namelab];
    self.namelab = namelab;
    [namelab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.offset(0);
//        make.left.offset(kwt(17));
    }];
    
    UIView * line = [UIView new];
    line.backgroundColor = KcolorHex(@"#E1E1E1");
    [self.contentView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.offset(0);
        make.left.offset(kwt(15));
        make.right.offset(kwt(-15));
        make.height.offset(0.5);
    }];
    MJWeakSelf
    self.contentView.addClickBlock(^{
        [weakSelf click];
    });
}
- (void)setName:(NSString *)name {
    _name = name;
    self.namelab.text = name;
}
- (void)click{
    if(self.clickBlock){
        self.clickBlock(self.name);
    }
}
@end
